在Rust的世界中,編譯器是你的第一道防線,能在記憶體洩漏和類型不匹配發生之前就捕捉到它們。然而,編譯器無法理解你的 意圖。這正是 自動化測試 發揮作用,作為邏輯正確性的「安全網」。
1. 相輔相成的組合
雖然類型系統負責維護結構完整性,但測試則驗證功能行為。在一個 函式庫專案專案中,編譯器會確保你不會將字串傳遞給數學函數,但唯有測試才能確保 $f(x) = y$ 的結果符合預期。
2. 標準生命週期
Rust 的測試遵循嚴格的三步驟流程:
- 設定: 初始化資料(例如,建立函式庫實例)。
- 執行: 執行被檢驗的特定邏輯。
- 斷言: 使用如
assert_eq!等巨集來驗證狀態。
整合工具鏈支援 文件測試 (文件中的可執行範例)以及 基準測試 以確保你的程式碼始終保持極快的運行速度。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which attribute must be placed above a function to mark it as a test for the runner?
#[cfg(test)]#[test]#[verify]#[cargo::test]✅ Correct!
The #[test] attribute is the specific metadata that signals to the compiler to include the function in the test runner binary.❌ Incorrect
While #[cfg(test)] is used for modules, individual functions require the #[test] attribute.QUESTION 2
How does Rust primarily signal that a test has failed?
The function returns
false.The function executes a
return Error;.The function thread panics.
The compiler issues a warning.
✅ Correct!
In Rust, a test fails if the logic triggers a panic (usually via assertions).❌ Incorrect
Rust tests look for panics to identify failure, though they can also return a Result.QUESTION 3
Why is
#[cfg(test)] used on the tests module?To make the tests run faster.
To ensure tests are only compiled when running
cargo test.To allow the module to access private variables.
To export the tests to other crates.
✅ Correct!
This conditional compilation attribute ensures your production binary doesn't include the testing code, keeping it lean.❌ Incorrect
It is a configuration hint that prevents test code from leaking into the 'cargo build' output.QUESTION 4
Which command both compiles and executes the test runner binary?
cargo build --testcargo testrustc test.rscargo run tests✅ Correct!
cargo test is the unified command that handles building the test binary and running it automatically.❌ Incorrect
cargo build only compiles; cargo test is needed to execute the results.QUESTION 5
What is the key difference between Doc-tests and Unit tests?
Doc-tests are only for documentation and cannot fail.
Unit tests live in code files; Doc-tests live inside triple-backtick comments.
Doc-tests are written in HTML, not Rust.
There is no difference.
✅ Correct!
Doc-tests are a unique Rust feature where code examples in your documentation comments are compiled and tested as if they were source code.❌ Incorrect
Doc-tests are actual Rust code embedded in comments to ensure README/documentation examples remain valid.Inventory Logic Shield
Case Study: Library Management System
You are developing a library management crate. While the type system ensures you don't enter a negative number for initial inventory, it cannot prevent a logic bug where borrowing a book accidentally increments the stock instead of decrementing it.
Q
1. What would be the 'Setup' phase for testing a 'borrow_book' function?
Solution:
Creating a new instance of the library struct and populating it with at least one book in the inventory (e.g., initial count = 5).
Creating a new instance of the library struct and populating it with at least one book in the inventory (e.g., initial count = 5).
Q
2. Which assertion macro would you use to verify the inventory after the 'Run' phase?
Solution:
assert_eq!(library.stock_count(), 4); This compares the actual count after execution with the expected decremented value.